#include "logger.h"
using namespace std;
using namespace log4cpp;

void Logger::warn(const char * file, int line, const char * func, const char * msg){
    _root->warn("[%s:%d] %s: %s", file, line, func, msg);
}
void Logger::error(const char * file, int line, const char * func, const char * msg){
    _root->error("[%s:%d] %s: %s", file, line, func, msg);
}
void Logger::debug(const char * file, int line, const char * func, const char * msg){
    _root->debug("[%s:%d] %s: %s", file, line, func, msg);
}
void Logger::info(const char * file, int line, const char * func, const char * msg){
    _root->info("[%s:%d] %s: %s", file, line, func, msg);
}

Logger * Logger::getInstance(){
    if(_pInstance == nullptr){
        _pInstance = new(Logger);
        //设置日志布局
        PatternLayout *o_pt = new PatternLayout();
        o_pt->setConversionPattern("%d [%p] - %m%n");
        PatternLayout *file_pt = new PatternLayout();
        file_pt->setConversionPattern("%d [%p] - %m%n");

        //设置输出器
        //输出到屏幕
        OstreamAppender *os = new OstreamAppender("console", &cout);
        os->setLayout(o_pt);

        //输出到日志文件
        FileAppender *fi =new FileAppender("defalt","../log/system.log");
        fi->setLayout(file_pt);

        //设置根模块日志记录器,并且输出到控制台和文件
        _root = & Category::getRoot();
        _root->addAppender(os);
        _root->addAppender(fi);
        _root->setPriority(Priority::DEBUG);
        return _pInstance;
    }
    return _pInstance;
}
void Logger:: destroy(){
    if(_pInstance != nullptr){
        delete _pInstance;
        Category::shutdown();
        _pInstance = nullptr;
        _root = nullptr;
    }

}

Logger:: Logger(){

}

Logger::~Logger(){
}
Logger * Logger::_pInstance = nullptr;
Category * Logger::_root = nullptr;

